<?php
	class NewsModel extends Model {
		function NewsModel()
		{
			parent::Model();			
		}
		
		function addNews($data)
		{
			$data = array(
               'title' => $data['title'] ,
			   'intro' => $data['intro'] ,
               'detail' => $data['detail'] ,
			   'show_main_page' => $data['show_main_page'],
			'show_edu' => $data['show_edu'],
			   'show_level_anuban' => $data['show_level_anuban'],
			   'show_level_pratom' => $data['show_level_pratom'],
			   'show_level_mo_ton' => $data['show_level_mo_ton'],
			   'show_level_mo_pay' => $data['show_level_mo_pay'],			  
			   'show_level_general' => $data['show_level_general'],
				'show_tutor' => $data['show_tutor'],					
			   'create_by' => $data['create_by'],
			   'is_active' => 1
            );
            $this->db->set('create_date', 'NOW()', FALSE); 
			$this->db->insert('news', $data);
			return $this->db->insert_id();
		}
		
		function updateFilePath($d)
		{			
			$data = array(
               'att_file' => $d['att_file']			   
            );
            $this->db->where('news_id', $d['news_id']);
			$this->db->update('news', $data); 
		}
		
		function updateImagePath($d)
		{			
			$data = array(
               'att_image' => $d['att_image']			   
            );
            $this->db->where('news_id', $d['news_id']);
			$this->db->update('news', $data); 
		}
		
		function updateNews($data)
		{			
			$dataDB = array(
              'title' => $data['title'] ,
               'detail' => $data['detail'] ,
			   'intro' => $data['intro'] ,
			   'show_main_page' => $data['show_main_page'],
			'show_edu' => $data['show_edu'],
			   'show_level_anuban' => $data['show_level_anuban'],
			   'show_level_pratom' => $data['show_level_pratom'],
			   'show_level_mo_ton' => $data['show_level_mo_ton'],
			   'show_level_mo_pay' => $data['show_level_mo_pay'],			  
			   'show_level_general' => $data['show_level_general'],
				'show_tutor' => $data['show_tutor'],			
			   'update_by' => $data['update_by']
            );
            $this->db->set('update_date', 'NOW()', FALSE); 
            $this->db->where('news_id', $data['news_id']);
			$this->db->update('news', $dataDB); 
		}
		
		function getTopTenNews()
		{
			$this->db->order_by("news_id", "desc"); 
			$query = $this->db->get_where('news', array('is_active' => 1, 'show_main_page' => 'Y'), 10);
			return $query->result();
		}
		
		function getTutorNews()
		{
			$this->db->order_by("news_id", "desc"); 
			$query = $this->db->get_where('news', array('is_active' => 1, 'show_edu' => 'Y'), 10);
			return $query->result();
		}
		
		function getNews($limit = NULL, $offset = NULL)
		{
			$this->db->limit($limit, $offset);
			$this->db->order_by("news_id", "desc"); 
			$query = $this->db->get_where('news', array('is_active' => 1, 'show_edu' => 'Y'));
			return $query->result();
		}
		
		function count_all()
	 	{
	 		$query = $this->db->getwhere('news', array('is_active'=>1, 'show_edu' => 'Y'));
			$count = $query->num_rows(); 
	  		return $count;
	 	}
		
		function getNewsByID($user)
		{
			$query = $this->db->get_where('news', array('news_id' => $user, 'is_active' => 1));
			return $query->row_array();
		}
		
		function getNewsByTitle($user)
		{
			$this->db->like('title', $user); 
			$this->db->order_by("news_id", "asc"); 
			$query = $this->db->get_where('news', array('is_active' => 1));
			return $query->result();
		}
		
		function deleteNews($newsID)
		{
			$data = array(
               'is_active' => 0,
			   'update_by' => $this->session->userdata('username')   
            );
            $this->db->set('update_date', 'NOW()', FALSE); 
            $this->db->where('news_id', $newsID);
			$this->db->update('news', $data); 
		}
	}
?>